<?php

/**
 * 腾讯im
 * @author hexuanlin
 * @version  $Revision 1.0  2020-09-29 12:28
 */

namespace app\helper;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class Mail{
    private $handle;
    public function __construct()
    {
        $this->handle = new PHPMailer(true); // Passing `true` enables exceptions
        //服务器配置
        $this->handle->CharSet ="UTF-8";                     //设定邮件编码
        $this->handle->SMTPDebug = 0;                        // 调试模式输出
        $this->handle->isSMTP();                             // 使用SMTP
        $this->handle->SMTPAuth = true;                      // 允许 SMTP 认证
        $this->handle->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议
        $this->setHost();
        $this->setUsername();
        $this->setPassword();
        $this->setPort();
    }
    public function setHost($value = 'smtp.sina.net'){
        $this->handle->Host = $value;
    }
    // SMTP 用户名  即邮箱的用户名
    public function setUsername($value = 'lishi@lishifamily.com'){
        $this->handle->Username = $value;
    }
    // SMTP 密码  部分邮箱是授权码(例如163邮箱)
    public function setPassword($value = '709a7dc7aab4d3f2'){
        $this->handle->Password = $value;
    }
    // 服务器端口 25 或者465 具体要看邮箱服务器支持
    public function setPort($value = '465'){
        $this->handle->Port = $value;
    }
    public function send($to, $file, $title = '',  $form = 'lishi@lishifamily.com'){
        $this->handle->setFrom($form, '小奇迹科技');  //发件人
        $this->handle->addAddress($to, 'Joe');  // 收件人
        //$this->handle->addAddress('ellen@example.com');  // 可添加多个收件人
        $this->handle->addReplyTo($form, 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致
        //$this->handle->addCC('cc@example.com');                    //抄送
        //$this->handle->addBCC('bcc@example.com');                    //密送

        //发送附件
        $this->handle->addAttachment($file);         // 添加附件
        // $this->handle->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名

        //Content
        $this->handle->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
        $this->handle->Subject = $title;
        $this->handle->Body    = '内容请查看附件';
        $this->handle->AltBody = '如果邮件客户端不支持HTML则显示此内容';

        $this->handle->send();
    }
}
